package com.hhkj.study.admin.goods.service.biz.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hhkj.study.admin.dict.entity.DictMultiLevel;
import com.hhkj.study.admin.dict.service.base.DictMultiLevelService;
import com.hhkj.study.admin.goods.entity.GoodsSku;
import com.hhkj.study.admin.goods.model.podo.GoodsSkuDo;
import com.hhkj.study.admin.goods.model.podo.GoodsSkuPo;
import com.hhkj.study.admin.goods.model.req.GoodsSkuInitReq;
import com.hhkj.study.admin.goods.model.req.GoodsSkuReq;
import com.hhkj.study.admin.goods.model.res.GoodsSkuRes;
import com.hhkj.study.admin.goods.service.base.GoodsSkuService;
import com.hhkj.study.admin.goods.service.biz.GoodsSkuBizService;
import com.hhkj.study.admin.goods.service.extend.GoodsSkuExtendService;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.enums.DictCodeEnum;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* GoodsSkuBizServiceImpl
*
* @author SERVER
* @since 2024-09-18
*/
@Service
public class GoodsSkuBizServiceImpl extends BaseBizServiceImpl<GoodsSku, GoodsSkuReq, GoodsSkuRes, GoodsSkuDo
        , GoodsSkuPo, GoodsSkuService, GoodsSkuExtendService> implements GoodsSkuBizService {

    @Resource
    private DictMultiLevelService dictMultiLevelService;

    @Override
    public List<GoodsSkuRes> init(GoodsSkuInitReq req) {
        List<DictMultiLevel> dictMultiLevelByNameCodeList = dictMultiLevelService
                .list(Wrappers.<DictMultiLevel>lambdaQuery().eq(DictMultiLevel::getTypeCode
                , DictCodeEnum.GOODS_SKU.getCode()).eq(DictMultiLevel::getName, req.getCourseTypeName()));
        if (CollectionUtil.isEmpty(dictMultiLevelByNameCodeList)) {
            throw new GlobalException("没有找到课程类型对应的初始化数据");
        }
        LambdaQueryWrapper<DictMultiLevel> dictQueryWrapper = Wrappers.<DictMultiLevel>lambdaQuery()
                .select(DictMultiLevel::getId, DictMultiLevel::getName)
                .eq(DictMultiLevel::getParentId, CollectionUtil.getFirst(dictMultiLevelByNameCodeList).getId())
                .eq(DictMultiLevel::getDisableFlag, SysConst.Flag.NO.getFlag())
                .eq(DictMultiLevel::getTypeCode, DictCodeEnum.GOODS_SKU.getCode())
                .orderByAsc(DictMultiLevel::getOrderNum);
        List<DictMultiLevel> dictMultiLevelList = dictMultiLevelService.list(dictQueryWrapper);
        if (CollectionUtil.isEmpty(dictMultiLevelList)) {
            throw new GlobalException("没有找到课程类型对应的商品sku");
        }
        return dictMultiLevelList.stream().map(dictMultiLevel -> {
            GoodsSkuRes goodsSkuRes = new GoodsSkuRes();
            goodsSkuRes.setName(dictMultiLevel.getName());
            return goodsSkuRes;
        }).collect(Collectors.toList());
    }
}